package service

import (
	"gitee.com/ling1314/origin-utility/model"
	"gitee.com/ling1314/origin-utility/modules/repository"
	"gitee.com/ling1314/origin-utility/utils"
)

type idpOrgService struct {
	baseService
}

func (s idpOrgService) GetOrgList(uid int64) (li []model.IdpOrg, err error) {
	return repository.IdpOrgRepository.FindOrg(uid)
}

func (s idpOrgService) UpdateOrg(m model.IdpOrg) (err error) {
	return repository.IdpOrgRepository.Update(&m)
}

func (s idpOrgService) SavePoint(pic, jsonStr string, orgId int64) (err error) {
	var (
		orgPoint model.IdpOrgPoint
		org      model.IdpOrg
	)
	if orgPoint, err = repository.IdpOrgRepository.FindOrgPoint(pic); err != nil {
		return err
	}
	if orgPoint.Id == 0 {
		orgPoint.Id = utils.GetSoftId()
	}
	orgPoint.PlanePic = pic
	orgPoint.JsonStr = jsonStr

	if org, err = repository.IdpOrgRepository.FindOrgById(orgId); err != nil {
		return err
	}
	org.PlanePic = pic
	if err = repository.IdpOrgRepository.Update(&org); err != nil {
		return err
	}
	return repository.IdpOrgRepository.SaveOrgPoint(&orgPoint)
}

func (s idpOrgService) GetPoint(pic string) (m model.IdpOrgPoint, err error) {
	return repository.IdpOrgRepository.FindOrgPoint(pic)
}
